/*
JETware Hands-free Extension provides additional features for Windows Mobile HFP support.
Copyright (C) 2004-2009 Jim Thatcher

This program is free software; you can redistribute it and/or modify it under the terms of the 
GNU General Public License as published by the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; 
if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Source Control header

$Workfile: DebugLog.cpp $
$Revision: 1 $
$Date: 1/1/09 1:11p $
*/
#include <windows.h>

#ifdef DEBUG

void LogString(LPSTR szCommand)
{
	SYSTEMTIME st;
	FILETIME ft;
	HANDLE h = NULL;
	char szTime[16];
	TCHAR szPath[MAX_PATH];
	HMODULE hMod = NULL;
	DWORD dwRet;

	lstrcpy(szPath, _T("JetBTConfig.exe")),		
	hMod = GetModuleHandle(szPath);
	dwRet = GetModuleFileName(hMod, szPath, MAX_PATH);

	if (dwRet)
	{
		TCHAR  *cSlash = wcsrchr(szPath, '\\');
		*cSlash = 0;
		lstrcat(szPath, L"\\BTConfig.txt");
	}
	else
	{
		BOOL bRet = SHGetSpecialFolderPath(NULL, szPath, CSIDL_PROGRAM_FILES, FALSE);
		lstrcat(szPath, L"\\JETware\\BTConfig.log");
	}


	h = CreateFile(szPath, GENERIC_WRITE, 0,
		NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	GetLocalTime(&st);
	SystemTimeToFileTime(&st, &ft);
	sprintf(szTime, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d:%2.2d - ", st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute, st.wSecond, ft.dwHighDateTime, ft.dwLowDateTime);
	if (h != INVALID_HANDLE_VALUE)
	{
		BOOL bResult;
		dwRet = SetFilePointer(h, 0, NULL, FILE_END);
		bResult = WriteFile(h, szTime, strlen(szTime), &dwRet, NULL);
		bResult = WriteFile(h, szCommand, strlen(szCommand), &dwRet, NULL);
		bResult = WriteFile(h, "\r\n", 2, &dwRet, NULL);
		CloseHandle(h);
	}
}

void LogBlob(void * pBlob, DWORD dwLen /*, DWORD wLevel */)
{
	// if (wLevel & g_dwDebugLevels)
	{
		CHAR * szLog = (CHAR *) LocalAlloc(LPTR, (dwLen+10)*3);
		DWORD dwCur = 0;
		CHAR szByte[4];
		CHAR szText[32];
		BYTE * pBytes = (BYTE *) pBlob;
		szLog[0] = 0;
		memset(szText, 0, sizeof(szText));
		while (dwCur < dwLen)
		{
			sprintf(szByte, "%2.2X ", *(pBytes+dwCur));
			strcat(szLog, szByte);
			if ((*(pBytes+dwCur) > 31) && (*(pBytes+dwCur) < 0x80))
				szText[(dwCur%16)] = *(pBytes+dwCur);
			else
				szText[(dwCur%16)] = '.';
			if ((dwCur%16) == 15)
			{
				strcat(szLog, "\t");
				strcat(szLog, szText);
				LogString(szLog);
				szLog[0] = 0;
				memset(szText, 0, sizeof(szText));
			}
			dwCur++;
		}
		
		for (int nEmpty = ((dwLen)%16); nEmpty < 16; nEmpty++)
		{
			strcat(szLog, "xx ");
		}
		strcat(szLog, "\t");
		strcat(szLog, szText);
		strcat(szLog, "\r\n");
		LogString(szLog);
		LocalFree(szLog);
	}
}

#endif